
package com.bi2.events.mobile;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class SearchEventsActivity extends Activity {

    private ListView events_listview;
    private EditText events_searchbox;
    private String lv_arr[] = {
            "Festa dos Plays", "São João da Capita", "Festa do Morro",
            "São João na Casa de Tia Maria"
    };
    private ArrayList<String> arr_sort = new ArrayList<String>();
    int textlength = 0;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.search_events);

        events_listview = (ListView) findViewById(R.id.events_search_listview);
        events_searchbox = (EditText) findViewById(R.id.search_events_searchbox);

        events_listview.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, lv_arr));
        events_searchbox.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {

                textlength = events_searchbox.getText().length();
                arr_sort.clear();
                for (int i = 0; i < lv_arr.length; i++) {
                    if (textlength <= lv_arr[i].length()) {
                        if (events_searchbox.getText().toString()
                                .equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
                            arr_sort.add(lv_arr[i]);
                        }
                    }
                }
                events_listview.setAdapter(new ArrayAdapter<String>(SearchEventsActivity.this,
                        android.R.layout.simple_list_item_1, arr_sort));
            }
        });
    }
}
